<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <form method="post" action="?" enctype="multipart/form-data">
        上传封面:<input type="file" name="myfile"/><br/><br/>
        <input type="hidden" value="1" name="go">
        <input type="submit" value="上传"/><br/><br/>
    </form>
</body>
</html>
<?php
if(!empty($_POST['go']) && $_POST['go']!==1){
    $fileinfo=$_FILES['myfile'];
    $filename=$fileinfo['name'];
    $tmp_name=$fileinfo['tmp_name'];
    $size=$fileinfo['size'];
    $error=$fileinfo['error'];
    $type=$fileinfo['type'];
    $maxsize=10485760;  
    $allowExt=array('jpeg', 'jpg', 'png', 'gif');
    $ext=pathinfo($filename, PATHINFO_EXTENSION); 
    $path = "./img/"; 

    if(!file_exists($path)) {  
        mkdir($path,0777,true); 
        chmod($path, 0777); 
    }
    $uniName=md5(uniqid(microtime(true),true)).".$ext";
    $destination=$path."/".$uniName; 

    if($error==0){
        if ($size>$maxsize) {
            exit ("上传文件过大！");
        }
        if (!in_array($ext, $allowExt)) {
            exit ("非法文件类型");
        }
        if (!is_uploaded_file($tmp_name)  ) {
            exit ("上传方式有误,请使用post方式");
        }
        if (!getimagesize($tmp_name)  ) {  
            exit ("不是真正的图片类型");
        }
        if (move_uploaded_file($tmp_name, $destination)) {
            echo "<script>parent.document.all.imgname.value = '$destination';</script>;
            文件".$filename."上传成功!";
        }else{
            echo "文件".$filename."上传失败!";
        }
    }else{
        switch ($error) {
            case 1:
                $max_upload = ini_get('upload_max_filesize');
                exit("文件大小超过php.ini限制(最大 { $max_upload})");
            case 2:
                exit("文件大小超过表单MAX_FILE_SIZE限制");
            case 3:
                exit("文件只有部分被上传，请重新上传");
            case 4:
                exit("没有选择要上传的文件");
            case 6:
                exit("服务器临时文件夹不可用");
            case 7:
                exit("文件写入失败，可能是磁盘空间不足");
            case 8:
                exit("文件上传被PHP扩展阻止");
            default:
                exit("上传错误（错误代码: { $error} )");
        }
    }
}
?>